<?php

require_once OUTER_PATH."auth/User.php";
require_once OUTER_PATH."mail/PHPMailer.php";
class mail_helper
{
	const MAIL_HOST = '121.14.48.194';
	const MAIL_SENDER = 'shijian@sdo.com';
	const MAIL_SENDER_NAME = '事件递交系统';
	const MAIL_DEFAULT_ACCEPTER = 'xucongbin@snda.com';
	const SERVICE_DESK_MAILER = 'land_gong@163.com';
	/**
	 * 
	 * 新建服务请求通知（用户）
	 * @param object $controller
	 * @param array $event 事件信息
	 * @param array $cc 抄送帐号
	 * @return boolean
	 */
	public static function newEventTOCustomerMail($controller, $event, $cc=array())
	{
		$deskMail = "";
		$title = "【新建请求提醒】".self::parseEventId($event['event_id'])."：{$event['event_title']}";
		$data['event'] = $event;
		$content = $controller->load->view("mail/newEventToCustomerMail", $data, true);
        $message = array();
        if (!User::AccNotify($message, $event['event_owner'], $title, $content, 'html')) {
        	return false;
        }
        $toRow = array('email'=>self::MAIL_DEFAULT_ACCEPTER, 'realname'=>$event['event_owner']);
		$ccList = array();
		if ($cc) {
        	foreach($cc as $user) {
        		$toRow = user_helper::getUserinfoByUserName($user);
        		if (!$toRow) {
        			continue;
        		}
        		$ccList[] = $toRow;
        	}
	    }
        #TODO
        $deskMail = array();
        $bcc = self::getServiceDeskMailer($controller);
		if ($bcc) {
        	foreach($bcc as $user) {
        		$toRow = user_helper::getUserinfoByUserName($user);
        		if (!$toRow) {
        			continue;
        		}
        		$deskMail[] = $toRow;
        	}
	    }
//        $deskMail = array(
//        	array('email'=>self::SERVICE_DESK_MAILER, 'realname'=>'服务台')
//        );
        
        self::sendMailBySMTP($title, $content, array($toRow), $ccList, $deskMail);
        return true;
	}
	/**
	 * 
	 * 新建服务请求通知（工作组）
	 * @param object $controller
	 * @param array $event 事件信息 array(
	 * 	event_group_name=>工作组名称,	
	 * 	...
	 * )
	 * @param array $cc 抄送帐号
	 * @return boolean
	 */
	public static function newEventTOGroupMail($controller, $event, $cc=array())
	{
		$deskMail = "";
		$title = "【新建请求提醒】".self::parseEventId($event['event_id'])."：{$event['event_title']}";
		$data['event'] = $event;
		$content = $controller->load->view("mail/newEventToGroupMail", $data, true);
		$toRow = user_helper::getUserinfoByUserName($event['event_charger']);
		if (!$toRow) {
			return false;
		}
		$ccList = array();
		if ($cc) {
        	foreach($cc as $user) {
        		$toRow = user_helper::getUserinfoByUserName($user);
        		if (!$toRow) {
        			continue;
        		}
        		$ccList[] = $toRow;
        	}
	    }
        
        #TODO
		$deskMail = array();
        $bcc = self::getServiceDeskMailer($controller);
		if ($bcc) {
        	foreach($bcc as $user) {
        		$toRow = user_helper::getUserinfoByUserName($user);
        		if (!$toRow) {
        			continue;
        		}
        		$deskMail[] = $toRow;
        	}
	    }
        self::sendMailBySMTP($title, $content, array($toRow), $ccList, $deskMail);
	}
	/**
	 * 
	 * 服务请求关闭通知（用户）
	 * @param object $controller
	 * @param array $event 事件信息
	 * @param array $cc
	 * @return boolean
	 */
	public static function closeEventTOCustomerMail($controller, $event, $cc=array())
	{
		$deskMail = "";
		$title = "【关闭请求提醒】".self::parseEventId($event['event_id'])."：{$event['event_title']}";
		$data['event'] = $event;
		$content = $controller->load->view("mail/closeEventToCustomerMail", $data, true);
        $message = array();
        if (!User::AccNotify($message, $event['event_owner'], $title, $content, 'html')) {
        	return false;
        }
        $toRow = array('email'=>self::MAIL_DEFAULT_ACCEPTER, 'realname'=>$event['event_owner']);
		if ($cc) {
        	foreach($cc as $user) {
        		$toRow = user_helper::getUserinfoByUserName($user);
        		if (!$toRow) {
        			continue;
        		}
        		$ccList[] = $toRow;
        	}
	    }
        #TODO
		$deskMail = array();
        $bcc = self::getServiceDeskMailer($controller);
		if ($bcc) {
        	foreach($bcc as $user) {
        		$toRow = user_helper::getUserinfoByUserName($user);
        		if (!$toRow) {
        			continue;
        		}
        		$deskMail[] = $toRow;
        	}
	    }
        self::sendMailBySMTP($title, $content, array($toRow), $ccList, $deskMail);
        return true;
	}
	
	public static function parseEventId($eventId)
	{
		return "SJ".str_pad($eventId, 8, '0', STR_PAD_LEFT);
	}
	
	public static function getServiceDeskMailer($controller)
	{
		$cc = array();
		$controller->load->model('groups');
		$chargerList = $controller->groups->getGroupMembersByGroupId(4);
		foreach($chargerList as $row) {
			if ($row['worker_name']!=$eventOwner) {
				$cc[] = $row['worker_name'];
			}
		}
		return $cc;
	}
	
	public static function sendMailBySMTP($title, $content, $to, $cc=array(), $bcc=array())
	{
		$phpmailerlite = new PHPMailer();
        try {
          $mailHost = self::MAIL_HOST;
          $mailWebSender = self::MAIL_SENDER;
          $phpmailerlite->CharSet = "UTF-8";
          $phpmailerlite->IsSMTP();
          $phpmailerlite->IsHTML(true);
          $phpmailerlite->SMTPAuth = false;
          $phpmailerlite->Host = $mailHost;
          $phpmailerlite->SetFrom($mailWebSender, self::MAIL_SENDER_NAME, false);
          $phpmailerlite->AddReplyTo("no-reply@snda.com", "no-reply");
          foreach($to as $user) {
          	if(is_array($user)) {
        		$phpmailerlite->AddAddress($user['email'], $user['realname']); 
        	} else {
        		$phpmailerlite->AddAddress($user); 
        	} 
          }
          $phpmailerlite->AddAddress($toRow['email'], $toRow['realname']); 	  
	      if ($cc) {
        	foreach($cc as $user) {
        		if(is_array($user)) {
        			$phpmailerlite->AddCC($user['email'], $user['realname']); 
        		} else {
        			$phpmailerlite->AddCC($user); 
        		}        		
        	}
	      }
          if ($bcc) {
        	foreach($bcc as $user) {
        		if(is_array($user)) {
        			$phpmailerlite->AddBCC($user['email'], $user['realname']); 
        		} else {
        			$phpmailerlite->AddBCC($user); 
        		}        		
        	}
	      }
          $phpmailerlite->Subject = $title;
          $phpmailerlite->Body = $content;
          $phpmailerlite->Send();
          $result = TRUE;
        }
        catch (phpmailerException $e) {
//          echo $e->getMessage();
          $result = FALSE;
        } catch (Exception $e) {
//           echo $e->getMessage();
          $result = FALSE;
        }
        
        #TODO
        
        return $result;
	}
}